layui.define(['jquery', 'kaBgUrl'],
    function (exports) {
        var $ = layui.jquery;
        var kaBgUrl = layui.kaBgUrl;
        var timeout = 10000;
        var kaAjax = new function () {
            this.request = function (url, method, data = {}, contentType, back, async, loadBoolean, tokenBoolean) {
                data["frontEndUrl"] = kaCtx.location();
                var requestOption = {
                    url: url,
                    type: method,
                    cache: false,
                    async: async, //同步 false
                    timeout: timeout,
                    contentType: getKaContentType(contentType),
                    dataType: getKaDataType(contentType),
                    loadLayer: 0,
                    beforeSend: function (xhr) {
                        if (loadBoolean) {
                            var loadLayerIndex = layui.kaMsg.loading();
                            this.loadLayer = loadLayerIndex;
                        }
                        if (tokenBoolean) {
                            xhr.setRequestHeader("Authorization", 'Bearer ' + layui.kaToken.get());
                        }
                    },
                    success: function (res) {
                        var loadLayerIndex = this.loadLayer;
                        if (loadLayerIndex > 0) {
                            layui.kaMsg.close(loadLayerIndex);
                        }
                        if (kaBgUrl.url(kaLogUrl).url != url) {
                            layui.kaLog.print("success", ["请求方式(url):", url]);
                            layui.kaLog.print("success", ["请求方式(method):", method]);
                            layui.kaLog.print("success", ["是否异步(async):", async]);
                            layui.kaLog.print("success", [{ "请求URL": url, "数据": res }]);
                        }
                        if (res.code == 200) {
                           return typeof back == "function" && back(res);
                        } else {
                            if(res.msg != undefined && res.msg != null && res.msg != "" && res.msg.indexOf("认证失败，无法访问系统资源") > -1){
                                top.location.href = "../login.html";
                            } else {
                                return typeof back == "function" && back(res);
                            }
                        }
                    },
                    error: function (error) {
                        var loadLayerIndex = this.loadLayer;
                        if (loadLayerIndex > 0) {
                            layui.kaMsg.close(loadLayerIndex);
                        }
                        layui.kaMsg.error("请求失败", JSON.stringify(error));
                        if (kaBgUrl.url(kaLogUrl).url != url) {
                            layui.kaLog.print("error", ["请求方式(url):", url]);
                            layui.kaLog.print("error", ["请求方式(method):", method]);
                            layui.kaLog.print("error", ["是否异步(async):", async]);
                        }
                        return typeof back == "function" && back(null);
                    }
                };
                var urlMethod = method.toLowerCase();
                if(urlMethod == "get") {
                    var symbol = url.indexOf('?') !== -1 ? '&' : '?';
                    var mb = "";
                    for (var key in data) {
                        if (mb == "") { 
                            mb = key + "=" + data[key];
                        } else {
                            mb = mb + "&" + key + "=" + data[key];
                        }
                    }
                    requestOption["url"] = requestOption["url"] + symbol + mb
                } else {
                   requestOption["data"] = JSON.stringify(data);
                }
                $.ajax(requestOption);
            }
            this.get = function (url, contentType, data, callBack, async, loadBoolean, tokenBoolean) {
                this.request(url, "GET", data, contentType, function (res) {
                    return typeof callBack == "function" && callBack(res)
                }, async, loadBoolean, tokenBoolean)
            }
            this.post = function (url, contentType, data, callBack, async, loadBoolean, tokenBoolean) {
                this.request(url, "POST", data, contentType, function (res) {
                    return typeof callBack == "function" && callBack(res)
                }, async, loadBoolean, tokenBoolean)
            }
            this.put = function (url, contentType, data, callBack, async, loadBoolean, tokenBoolean) {
                this.request(url, "PUT", data, contentType, function (res) {
                    return typeof callBack == "function" && callBack(res)
                }, async, loadBoolean, tokenBoolean)
            }
            this.delete = function (url, data,  async, callBack, loadBoolean, tokenBoolean) {
                this.request(url, "DELETE", data, "json", function (res) {
                    return typeof callBack == "function" && callBack(res)
                }, async, loadBoolean, tokenBoolean)
            }
            this.ajax = function (ajaxUrl, back, data) {
                if (data == undefined) { data = {}; }
                if (ajaxUrl != undefined) {
                    if((ajaxUrl["cs1"] != undefined && ajaxUrl["cs1"] != null && ajaxUrl["cs1"] != "")){
                        let match = ajaxUrl["cs1"].match(/\{(.*?)\}/);
                        if (match) {
                            ajaxUrl["url"] = ajaxUrl["url"] + "/" + data[match[1]];
                        }
                    }
                    if((ajaxUrl["cs2"] != undefined && ajaxUrl["cs2"] != null && ajaxUrl["cs2"] != "")){
                        let match = ajaxUrl["cs2"].match(/\{(.*?)\}/);
                        if (match) {
                            ajaxUrl["url"] = ajaxUrl["url"] + "/" + data[match[1]];
                        }
                    }
                    var loadBoolean = ajaxUrl.loadboolean;
                    var tokenBoolean = ajaxUrl.tokenboolean;
                    var async = ajaxUrl.async;
                    if (async == undefined || async == "true") { async = true; } else if(async == "false") { async = false; }
                    if (loadBoolean == undefined || loadBoolean == "false") { loadBoolean = false; } else if(loadBoolean == "true") { loadBoolean = true; }
                    if (tokenBoolean == undefined || tokenBoolean == "false") { tokenBoolean = false; } else if(tokenBoolean == "true") { tokenBoolean = true; }
                    if (ajaxUrl.methodType == "GET") {
                        kaAjax.get(ajaxUrl.url, ajaxUrl.contentType, data, back, async, loadBoolean, tokenBoolean);
                    }
                    if (ajaxUrl.methodType == "POST") {
                        kaAjax.post(ajaxUrl.url, ajaxUrl.contentType, data, back, async, loadBoolean, tokenBoolean);
                    }
                    if (ajaxUrl.methodType == "PUT") {
                        kaAjax.put(ajaxUrl.url, ajaxUrl.contentType, data, back, async, loadBoolean, tokenBoolean);
                    }
                    if (ajaxUrl.methodType == "DELETE") {
                        kaAjax.delete(ajaxUrl.url, data, async, back, loadBoolean, tokenBoolean);
                    }
                }
            }
        };
        exports('kaAjax', kaAjax);
    }
)